// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   comment_check_ok.java

package draft.ecommerce;

import com.jspsmart.upload.*;
import include.get_sql.getInsertSql;
import include.nseer_cookie.*;
import include.nseer_db.nseer_db_backup1;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataNumber;
import validata.ValidataTag;

public class comment_check_ok extends HttpServlet
{

	public comment_check_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		try
		{
			HttpSession dbSession = request.getSession();
			JspFactory _jspxFactory = JspFactory.getDefaultFactory();
			javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
			ServletContext dbApplication = dbSession.getServletContext();
			HttpSession session = request.getSession();
			ServletContext context = session.getServletContext();
			String path = context.getRealPath("/");
			nseer_db_backup1 ecommerce_db = new nseer_db_backup1(dbApplication);
			ValidataNumber validata = new ValidataNumber();
			ValidataTag vt = new ValidataTag();
			counter count = new counter(dbApplication);
			getInsertSql getInsertSql = new getInsertSql();
			getAttachmentLength getAttachmentLength = new getAttachmentLength();
			getAttachmentType getAttachmentType = new getAttachmentType();
			SmartUpload mySmartUpload = new SmartUpload();
			mySmartUpload.setCharset("UTF-8");
			if (ecommerce_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				mySmartUpload.initialize(pageContext);
				String file_type = getFileLength.getFileType((String)session.getAttribute("unit_db_name"));
				long d = getFileLength.getFileLength((String)session.getAttribute("unit_db_name"));
				mySmartUpload.setMaxFileSize(d);
				mySmartUpload.setAllowedFilesList(file_type);
				String config_ID = request.getParameter("config_id");
				String comment_ID = request.getParameter("comment_ID");
				try
				{
					mySmartUpload.upload();
					String name = mySmartUpload.getRequest().getParameter("name");
					String sex = mySmartUpload.getRequest().getParameter("sex");
					String company = mySmartUpload.getRequest().getParameter("company");
					String address = mySmartUpload.getRequest().getParameter("address");
					String tel = mySmartUpload.getRequest().getParameter("tel");
					String email = mySmartUpload.getRequest().getParameter("email");
					String kind = mySmartUpload.getRequest().getParameter("kind");
					String write_time = mySmartUpload.getRequest().getParameter("write_time");
					String comment = new String(mySmartUpload.getRequest().getParameter("comment").getBytes("UTF-8"), "UTF-8");
					String bodya = new String(mySmartUpload.getRequest().getParameter("reply").getBytes("UTF-8"), "UTF-8");
					String reply = exchange.toHtml(bodya);
					String checker_ID = mySmartUpload.getRequest().getParameter("checker_ID");
					String checker = mySmartUpload.getRequest().getParameter("checker");
					String check_time = mySmartUpload.getRequest().getParameter("check_time");
					String file_name[] = new String[mySmartUpload.getFiles().getCount()];
					String not_change[] = new String[mySmartUpload.getFiles().getCount()];
					if (vt.validata((String)session.getAttribute("unit_db_name"), "ecommerce_comment", "comment_ID", comment_ID, "check_tag").equals("9") || vt.validata((String)session.getAttribute("unit_db_name"), "ecommerce_comment", "comment_ID", comment_ID, "check_tag").equals("5"))
					{
						String sql1 = (new StringBuilder("select * from ecommerce_comment where comment_ID='")).append(comment_ID).append("'").toString();
						ResultSet rs = ecommerce_db.executeQuery(sql1);
						rs.next();
						String attachment[] = mySmartUpload.getRequest().getParameterValues("attachment");
						String delete_file_name[] = new String[0];
						if (attachment != null)
						{
							delete_file_name = new String[attachment.length];
							for (int i = 0; i < attachment.length; i++)
								delete_file_name[i] = rs.getString(attachment[i]);

						}
						for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++)
						{
							SmartFile file = mySmartUpload.getFiles().getFile(i);
							if (file.isMissing())
							{
								file_name[i] = "";
								int q = i + 1;
								String field_name = (new StringBuilder("attachment")).append(q).toString();
								if (!rs.getString(field_name).equals(""))
									not_change[i] = "yes";
							} else
							{
								int filenum = count.read((String)dbSession.getAttribute("unit_db_name"), "ecommerceAttachmentcount");
								count.write((String)dbSession.getAttribute("unit_db_name"), "ecommerceAttachmentcount", filenum);
								file_name[i] = (new StringBuilder(String.valueOf(filenum))).append(file.getFileName()).toString();
								file.saveAs((new StringBuilder(String.valueOf(path))).append("ecommerce\\file_attachments\\").append(filenum).append(file.getFileName()).toString());
							}
						}

						String sqla = (new StringBuilder("update ecommerce_comment set kind='")).append(kind).append("',name='").append(name).append("',sex='").append(sex).append("',company='").append(company).append("',address='").append(address).append("',tel='").append(tel).append("',email='").append(email).append("',comment='").append(comment).append("',reply='").append(reply).append("'").toString();
						String sqlb = (new StringBuilder(" where comment_ID='")).append(comment_ID).append("'").toString();
						if (attachment != null)
						{
							for (int i = 0; i < attachment.length; i++)
							{
								sqla = (new StringBuilder(String.valueOf(sqla))).append(",").append(attachment[i]).append("=''").toString();
								File file = new File((new StringBuilder(String.valueOf(path))).append("ecommerce\\file_attachments\\").append(delete_file_name[i]).toString());
								file.delete();
							}

						}
						for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++)
							if (not_change[i] == null || !not_change[i].equals("yes"))
							{
								int p = i + 1;
								sqla = (new StringBuilder(String.valueOf(sqla))).append(",attachment").append(p).append("='").append(file_name[i]).append("'").toString();
							}

						String sql = (new StringBuilder(String.valueOf(sqla))).append(sqlb).toString();
						ecommerce_db.executeUpdate(sql);
						List rsList = GetWorkflow.getList(ecommerce_db, "ecommerce_config_workflow", "08");
						String elem[] = new String[3];
						if (rsList.size() == 0)
						{
							sql = (new StringBuilder("update ecommerce_other set check_tag='1' where other_ID='")).append(comment_ID).append("'").toString();
							ecommerce_db.executeUpdate(sql);
						} else
						{
							sql = (new StringBuilder("delete from ecommerce_workflow where object_ID='")).append(comment_ID).append("'").toString();
							ecommerce_db.executeUpdate(sql);
							sql = (new StringBuilder("update ecommerce_comment set check_tag='0' where comment_ID='")).append(comment_ID).append("'").toString();
							ecommerce_db.executeUpdate(sql);
							for (Iterator ite = rsList.iterator(); ite.hasNext(); ecommerce_db.executeUpdate(sql))
							{
								elem = (String[])ite.next();
								sql = (new StringBuilder("insert into ecommerce_workflow(config_id,object_ID,describe1,describe2) values ('")).append(elem[0]).append("','").append(comment_ID).append("','").append(elem[1]).append("','").append(elem[2]).append("')").toString();
							}

						}
						response.sendRedirect("draft/ecommerce/comment_ok.jsp?finished_tag=4");
					} else
					{
						response.sendRedirect("draft/ecommerce/comment_ok.jsp?finished_tag=5");
					}
					ecommerce_db.commit();
					ecommerce_db.close();
				}
				catch (Exception ex)
				{
					response.sendRedirect("draft/ecommerce/comment_ok.jsp?finished_tag=6");
				}
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
